<?php
/*
*   视图统一化处理；即是将不同的模板封装起来
*   为 Hyang\Controller 提供支持
*/
namespace hyang\traits;
trait View{
    // 视图通过类
    private $_dataView = array();
    // 模板单值渲染- 支持回调函
    public function setVar($name,$value)
    {
        if(is_callable($value)) $value = $value($this);
        $this->_dataView[$name] = $value;
        return false;
    }    
    public function setVars($data)
    {
        if(!is_array($data)) return false;
        foreach($data as $k=>$v){
            $this->_dataView[$k] = $v;
        }
        return true;
    }
    // setVar/setVars 同名函数- 支持回调函
    public function assign($name,$value=null)
    {
        if(is_callable($value)) $value = $value($this);
        // For Smarty
        if($this->ViewEngineName == 'Smarty'){
            return $this->view->assign($name,$value);
        }
        if(is_array($name)){
            return $this->setVars($name);
        }else{
            return $this->setVar($name,$value);
        }
    }
    // 渲染
    public function render($tpl=null,$renderData=null)
    {
        $feek = false;
        if($tpl == 'FEEK'){
            $feek = true;
            $tpl = null;
        }
        $origalFile = $tpl;
        $name = $this->ViewEngineName;
        
        if(empty($tpl)){
            $tpl = _APP('Controller').'/'._APP('Action');
            if($name == 'Plates') ;
            elseif(defined('View_Suffix') && View_Suffix != "") $tpl .= '.'.View_Suffix;
            else $tpl .= '.html';
        }
        $tpl = _APP('Module').'/View/'.$tpl;
        $data = $this->getDataView();
        if(is_array($renderData)){
            if(is_array($data) && $data) $data = array_merge($data,$renderData);
            else $data = $renderData;
        }
        //if(empty($data)) return; bug 没有渲染数据时不阻止渲染     
        $view = $this->view;
        switch($name){
            case 'Mustache':
                $md = $view->loadTemplate($tpl);
                $content = $md->render($data);
                if($feek) return $content;
                echo $content;
                break;
            case 'Plates':
                $content = $view->render($tpl,$data);
                if($feek) return $content;
                echo $content;
                break;
            case 'Smarty':
                $view->assign($data);
                $tpl = is_file($origalFile)? $origalFile:APP_ROOT.Controller_Path.$tpl;
                $view->display($tpl);
                break;
            case 'Twig':
                $content = $view->render($tpl,$data);
                if($feek) return $content;
                echo $content;
                break;            
        }

    }
    //----------------------------------------------- 调试工具    
    public function getDataView(){return $this->_dataView;}         // 获取渲染值
    public function useViewEngine(){return $this->view;}            // 获取渲染器引擎
    public function getViewEngine(){return $this->ViewEngineName;}  // 获取渲染器引擎
    public function getVEVersion()                                  // 获取模板版本
    {
        $view = $this->view;
        $type = $this->getViewEngine();
        if(empty($type)) return '';
        switch($type){
            case 'Mustache': return $view::VERSION;
            case 'Smarty': return $view::SMARTY_VERSION;
            case 'Twig': return $view::VERSION;
            default:
                return '无法获取“'.$type.'”模板引擎的版本信息';
        }
    }

}